﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;

namespace RFIDStockManagement.Client.Admin
{
    public partial class UserEditerForm : DevExpress.XtraEditors.XtraForm
    {
        private UserInfo _editUserInfo;
        private UserManager _manager;
        private bool isAddUser;
        private bool isOperatOk;


        /// <summary>
        /// 是否操作成功
        /// </summary>
        public bool IsOperatOk
        {
            get { return isOperatOk; }
            set { isOperatOk = value; }
        }

        /// <summary>
        /// 当前正在编辑的用户信息
        /// </summary>
        public UserInfo EditUserInfo
        {
            get { return _editUserInfo; }
            set { _editUserInfo = value; }
        }

       

        public UserEditerForm(UserManager manager)
        {
            InitializeComponent();
            _manager = manager;
            if (_manager == null)
            {
                _manager = new UserManager();
            }
           
        }

        private void UserEditerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        public void AddUser()
        {
            IsOperatOk = false;
            layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            //txtPwd1.Visible = true;
            //txtPwd2.Visible = true;
            txtLogName.Enabled = true;

            txtLogName.Text = string.Empty;
            txtName.Text = string.Empty;
            txtPwd1.Text = string.Empty;
            txtPwd2.Text = string.Empty;
            comSex.SelectedIndex = 0;
            chkIsAdmin.Checked = false;
            chkIsAdmin.Enabled = true;
            txtTel.Text = string.Empty;
            isAddUser = true;
            this.ShowDialog();
        }

        public void ModifyUser(int userId)
        {
            IsOperatOk = false;
            _editUserInfo = _manager.GetUserById(userId);
            if (_editUserInfo == null)
            {
                AlertForm.ShowMessage("当前用户信息未找到,或已被删除.", "信息未找到");
                return;
            }

            if (_editUserInfo.LogName.Trim().ToLower() == "admin")
            {
                chkIsAdmin.Enabled = false;
            }
            else
            {
                chkIsAdmin.Enabled = true; ;
            }

            layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            //txtPwd1.Visible = false;
            //txtPwd2.Visible = false;
            txtLogName.Enabled = false;

            txtLogName.Text = _editUserInfo.LogName;
            txtName.Text = _editUserInfo.Name;
            txtPwd1.Text = _editUserInfo.Passwords;
            txtPwd2.Text = _editUserInfo.Passwords;
            comSex.SelectedIndex = _editUserInfo.IsMale?0:1;
            chkIsAdmin.Checked = _editUserInfo.IsAdmin;
            txtTel.Text = _editUserInfo.TelNumber;
            isAddUser = false;
            this.ShowDialog();
        }

        private UserInfo GetCurUserInfo()
        {
            if (_editUserInfo == null)
            {
                _editUserInfo = new UserInfo();
            }
            _editUserInfo.LogName = txtLogName.Text.Trim();
            _editUserInfo.Name = txtName.Text.Trim();
            _editUserInfo.Passwords = txtPwd1.Text.Trim();
            _editUserInfo.TelNumber = txtTel.Text.Trim();
            _editUserInfo.IsMale = comSex.SelectedIndex == 0;
            _editUserInfo.IsAdmin = chkIsAdmin.Checked;

            return _editUserInfo;
           
        }

        private bool CheckInput()
        {

            if (txtLogName.Text.Trim().Length <= 4)
            {
                AlertForm.ShowMessage("登录名长度必须大于4位", "操作失败");
                return false;
            }
            if (txtPwd1.Text.Trim().ToLower() != txtPwd2.Text.Trim().ToLower())
            {
                AlertForm.ShowMessage("两次输入的密码不一致,请确保密码和密码确认输入一样", "操作失败");
                return false;
            }
            if (txtLogName.Text.Trim().Length < 1)
            {
                AlertForm.ShowMessage("用户姓名必须输入", "操作失败");
                return false;
            }

            return true;
        }

        private void bntOk_Click(object sender, EventArgs e)
        {
            string errString;

            if (!CheckInput()) return;

            GetCurUserInfo();
            
            if (isAddUser)
            {
                isOperatOk = _manager.AddUser(_editUserInfo, out errString);
            }
            else
            {

                isOperatOk = _manager.ChangeUserInfo(_editUserInfo, out errString);
            }

           
            if (isOperatOk)
            {
               this.Hide();
               //AlertForm.ShowMessage("操作成功", "成功");
            }
            else
            {
                AlertForm.ShowMessage(errString, "操作失败");
            }

        }

        private void bntCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        
    }
}